﻿using System;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class PlanSeatMap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("NA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                if (Request.QueryString["flt_no"] == null)
                {
                    lblError.Text = "Invalid page call!";
                    return;
                }
                if (Request.QueryString["id"] == null)
                {
                    lblError.Text = "Invalid page call!";
                    return;
                }

                lblAirline.Text = Session["AIRLINE"].ToString();
                lblFltNo.Text = Request.QueryString["flt_no"].ToString();
                lblID.Text = Request.QueryString["id"].ToString();

                lbtnQuery_Click(sender, e);
            }
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnAdd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnChange.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            int ret_code;
            ret_code = flt_seat_map1.RetreiveSeatMap('P', lblAirline.Text, lblFltNo.Text, lblID.Text, Convert.ToDateTime("1900-1-1"));
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string[] strArray;
            string key;
            if (Request.Form["CELL"] == null)
            {
                lblError.Text = "Please select a record.";
                return;
            }
            key = Request.Form["CELL"];
            strArray = key.Split('|');

            int ret_code;

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_delete_seat_cell";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@plan_id", lblID.Text);
                para = cmd.Parameters.AddWithValue("@x", strArray[0]);
                para = cmd.Parameters.AddWithValue("@y", strArray[1]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    lbtnQuery_Click(sender, e);
                    return;
                }
                else if (ret_code == 1)
                    lblError.Text = "Deleting failed!";
                else if (ret_code == 3)
                    lblErrAdd.Text = "Plan is not in editing mode!";
                else
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblErrAdd.Text = "Unauthorized!";
                return;
            }
            txtSeatType.Text = txtSeatType.Text.Trim();
            if (txtSeatType.Text.Length == 0)
            {
                lblErrAdd.Text = "Please input reserve type!";
                return;
            }
            int ret_code;

            SqlCommand cmd = new SqlCommand();
            lblErrAdd.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_add_seat_cell";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@plan_id", lblID.Text);
                para = cmd.Parameters.AddWithValue("@x", txtX.Text);
                para = cmd.Parameters.AddWithValue("@y", txtY.Text);
                para = cmd.Parameters.AddWithValue("@cabin", lstAcCabins.SelectedValue);
                para = cmd.Parameters.AddWithValue("@cell_type", lstSpaceTypes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@row", txtRow.Text);
                para = cmd.Parameters.AddWithValue("@col", lstCols.SelectedValue);
                para = cmd.Parameters.AddWithValue("@load_area", lstLoadArea.SelectedValue);
                para = cmd.Parameters.AddWithValue("@seat_type", txtSeatType.Text.ToUpper());
                para = cmd.Parameters.AddWithValue("@class", lstClasses.SelectedValue);
                para = cmd.Parameters.AddWithValue("@attributes", txtAttributes.Text.ToUpper());
                para = cmd.Parameters.AddWithValue("@ori_airport", lstOriAirports.SelectedValue);
                para = cmd.Parameters.AddWithValue("@des_airport", lstDesAirports.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblErrAdd.Text = "Done.";
                    lbtnQuery_Click(sender, e);
                    return;
                }
                else if (ret_code == 1)
                    lblErrAdd.Text = "Inserting failed!";
                else if (ret_code == 3)
                    lblErrAdd.Text = "Plan is not in editing mode!";
                else if (ret_code == 4)
                    lblErrAdd.Text = "Seat number existed!";
                else if (ret_code == 5)
                    lblErrAdd.Text = "Y exceeded 15!";
                else
                    lblErrAdd.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrAdd.Text = ex.Message;
                else
                    lblErrAdd.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblErrChange.Text = "Unauthorized!";
                return;
            }
            if (lstEditTarget.SelectedValue == "S" || lstEditTarget.SelectedValue == "C")
            {
                if (txtNewValue.Text.Trim().Length > 2)
                {
                    lblErrChange.Text = "New value is too long!";
                    return;
                }
            }

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            lblErrChange.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_batch_set_seats_reserving";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@plan_id", lblID.Text);
                para = cmd.Parameters.AddWithValue("@change_target", lstEditTarget.SelectedValue);
                para = cmd.Parameters.AddWithValue("@new_value", txtNewValue.Text.Trim());
                para = cmd.Parameters.AddWithValue("@where_clause", txtWhere.Text);
                //para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblErrChange.Text = "Done.";
                    lbtnQuery_Click(sender, e);
                    return;
                }
                else if (ret_code == 1)
                    lblErrChange.Text = "Illegal verbs";
                else if (ret_code == 2)
                    lblErrChange.Text = "The aircraft is not belong to the airline!";
                else if (ret_code == 3)
                    lblErrChange.Text = "Invalid target!";
                else if (ret_code == 4)
                    lblErrChange.Text = "Updating failed!";
                else
                    lblErrChange.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrChange.Text = ex.Message;
                else
                    lblErrChange.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void lstAcCabins_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstClasses.DataBind();
        }
        protected void lstAirports_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstClasses.DataBind();
        }
        protected void lstClasses_DataBound(object sender, EventArgs e)
        {
            lstClasses.Items.Add(new ListItem("* - (No Control) ", "*"));
        }


        protected void lstOriAirports_DataBound(object sender, EventArgs e)
        {
            lstOriAirports.Items.Add(new ListItem("* - (No Control) ", "*"));
        }
        protected void lstDesAirports_DataBound(object sender, EventArgs e)
        {
            lstDesAirports.Items.Add(new ListItem("* - (No Control) ", "*"));
        }
    }
}